/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.example;

import net.sourceforge.openforecast.*;
import net.sourceforge.openforecast.Observation;
import net.sourceforge.openforecast.models.MultipleLinearRegressionModel;
import java.util.Hashtable;
import java.util.Iterator;

/**
 *
 * @author Nadeeshani Hewage
 * Undergraduate: University of Moratuwa, Sri Lanka
 **/
public class MLRforecast {

    DataSet ds1, ds2,forecastedDS;
    DataPoint dp1;
    MultipleLinearRegressionModel forecaster;
    Hashtable result;
    String[] coefficients;
    String[][] output;
    double[][] outputDouble;

    String[] independantVariableNames;
    double[] dependantVariableValues;



    MLRforecast(double[][] yVariable, double[][] xVariable, String[][] xVarNames, double[][] xVariablesForForcasting){

        independantVariableNames = new String[xVarNames[0].length];
                for(int i=0;i<xVarNames[0].length;i++){
                independantVariableNames[i]=xVarNames[0][i];
                }

        dependantVariableValues = new double[yVariable.length];
                for(int i=0;i<yVariable.length;i++){
                dependantVariableValues[i]=yVariable[i][0];
                }


        ds1= new DataSet();

        for (int i=0;i<dependantVariableValues.length;i++){
            dp1= new Observation(dependantVariableValues[i]);

            for(int j=0; j<independantVariableNames.length; j++){
                dp1.setIndependentValue(independantVariableNames[j],xVariable[i][j]);
            }
            ds1.add(dp1);
        }

        coefficients    = new String[independantVariableNames.length];           //string array for coefficients
	forecaster      = new MultipleLinearRegressionModel( independantVariableNames ); //MLReg Model

	// Initialize the model
	forecaster.init( ds1 );
        result= forecaster.getCoefficients();                           //get coefficients to the hashtable

        //Forecasting
        ds2=new DataSet();
        for (int i=0;i<xVariablesForForcasting.length;i++){
            dp1= new Observation(0);

            for(int j=0; j<independantVariableNames.length; j++){
                dp1.setIndependentValue(independantVariableNames[j],xVariablesForForcasting[i][j]);
            }
            ds2.add(dp1);
        }

        forecastedDS=forecaster.forecast(ds2);
        outputDouble= new double[xVariablesForForcasting.length][1];

    }


    double[][] returnResult(){

//        Collection col= result.values();
//        output=new String[independantVariableNames.length][1];

        //int index=0;
        //Iterator it = col.iterator();
//        while (it.hasNext()){
//
//            coefficients[index]=it.next().toString();
//            output[index][0]=coefficients[index];
//            //System.out.println(coefficients[index]);
//            index++;
//
//            //System.out.println(it.next());
//
//        }
        int indexDb=0;
        Iterator it = forecastedDS.iterator();

        while ( it.hasNext() )
            {
            DataPoint dp = (DataPoint)it.next();

            outputDouble[indexDb][0]=dp.getDependentValue();
            indexDb++;
            //double forecastValue = dp.getDependentValue();
            // Do something with the forecast value, e.g.
            //System.out.println( forecastValue );
            }

       return outputDouble;
    }




}
